package scala.Expression.For

object MainOB {
   def main(args: Array[String]): Unit = {

      //using for expression in many styles
      val ll = for (
         a <- books; b <- a.author if (b.startsWith("Gosling"))
      ) yield a.title

      println(ll)

      val l3 = for (a <- l1; b <- l2) yield (a, b)
      println(l3)

      val noDup1 = removeDuplicate_For(dupList)
      println(noDup1)
      val noDup2 = removeDuplicate_Normal(dupList)
      println(noDup2)
   }

   //define data
   val books: List[Book] = List(
      Book(
         "Structure and Interpretation of Computer Programs",
         "Abelson, Harold", "Sussman, Gerald J."),
      Book(
         "Principles of Compiler Design",
         "Aho, Alfred", "Ullman, Jeffrey"),
      Book(
         "Programming in Modula-2",
         "Wirth, Niklaus"),
      Book(
         "Elements of ML Programming",
         "Ullman, Jeffrey"),
      Book(
         "The Java Language Specification", "Gosling, James",
         "Joy, Bill", "Steele, Guy", "Bracha, Gilad"))

   val l1 = List(1, 2, 3, 4, 5)
   val l2 = List("a", "b", "c", "d", "e")
   val dupList = List(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)

   //remove duplicated list not using for
   def removeDuplicate_Normal[T](l: List[T]): List[T] = {
      l match {
         case List() => l
         case _ => {
            l.head :: removeDuplicate_Normal(l.tail.filter(x => x != l.head))
         }
      }
   }

   //remove duplicated list using for
   def removeDuplicate_For[T](xs: List[T]): List[T] = {
      xs match {
         case List() => xs
         case _      => xs.head :: removeDuplicate_For(for (x <- xs.tail if (x != xs.head)) yield x)
      }
   }

}

case class Book(title: String, author: String*)

/*
*
*
*
*
*
*/ 